package com.awo.facedetect.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class FaceRectView extends View {
    private Rect faceRect;

    private Paint paint;

    private int paintColor = Color.RED;

    // 默认人脸框厚度
    private static final int DEFAULT_FACE_RECT_THICKNESS = 2;

    public FaceRectView(Context context) { this(context, null); }

    public FaceRectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    public FaceRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
    }

    public FaceRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        paint = new Paint();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (faceRect != null &&  canvas != null) {
            @SuppressLint("DrawAllocation") Path mPath = new Path();
             //左上
            Rect rect = faceRect;
            mPath.moveTo(rect.left, rect.top + rect.height() / 4);
            mPath.lineTo(rect.left, rect.top);
            mPath.lineTo(rect.left + rect.width() / 4, rect.top);
            // 右上
            mPath.moveTo(rect.right - rect.width() / 4, rect.top);
            mPath.lineTo(rect.right, rect.top);
            mPath.lineTo(rect.right, rect.top + rect.height() / 4);
            // 右下
            mPath.moveTo(rect.right, rect.bottom - rect.height() / 4);
            mPath.lineTo(rect.right, rect.bottom);
            mPath.lineTo(rect.right - rect.width() / 4, rect.bottom);
            // 左下
            mPath.moveTo(rect.left + rect.width() / 4, rect.bottom);
            mPath.lineTo(rect.left, rect.bottom);
            mPath.lineTo(rect.left, rect.bottom - rect.height() / 4);
            canvas.drawPath(mPath, paint);

        }
    }

    public void updateFaceDrawInfo(Rect faceRect) {
        this.faceRect = faceRect;
        postInvalidate();
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    public void setPaintColor(int paintColor) {
        this.paintColor = paintColor;
    }

    public void update() {
        postInvalidate();
    }
}
